package com.pro.system.Repository;

import com.pro.system.dto.UserComment;
import com.pro.system.entity.Comment;
import com.pro.system.entity.Movie;
import com.pro.system.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

@Repository
public interface CommentRepository extends JpaRepository<Comment,Integer> {
    @Override
    List<Comment> findAll();

    @Modifying
    @Transactional
    @Query(value = "select * from user where name = ?1" ,nativeQuery = true)
    public List<User> findByName(String name);

    @Modifying
    @Transactional
    @Query(value = "select * from comment where content like %?1% " ,nativeQuery = true)
    public List<Comment> findByNameLike(String name);

    @Query(value ="select c.comment_id as commentId,c.content, c.date, c.movie_id as movieId, c.score, c.user_id as userId ,u.username " +
                  "from users as u inner join comment as c on u.user_id = c.user_id and c.movie_id = ?1 "
            , nativeQuery = true)
    public List<UserComment> findByMovieId(Integer id);



}
